package com.meitianhui.convenitenbuy.module.store.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.store.contract.StoreOneLotteryContract;
import com.meitianhui.convenitenbuy.module.store.http.StoreApiDataSource;
import com.meitianhui.convenitenbuy.module.store.model.PayInfoBean;
import com.meitianhui.convenitenbuy.module.store.model.StoreOneLotteryBean;
import com.meitianhui.convenitenbuy.module.store.ui.activity.OneLotteryActivity;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/27.
 */

public class StoreOneLotteryPresenter implements StoreOneLotteryContract.Presenter {

    private OneLotteryActivity mContext;
    private final StoreOneLotteryContract.View mView;
    private Gson gson;

    public StoreOneLotteryPresenter(OneLotteryActivity view,
                                    OneLotteryActivity context) {
        this.mView = view;
        this.mContext = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getOneLottery(String storeId) {
        String appToken = InfoProvider.getAppToken(App.getInstance());
        String service = "goods.ldActivityListByStoresForConsumerPageFind";
        Map<String, String> map = new HashMap<>();
        map.put("stores_id", storeId);

        String params = gson.toJson(map);
        StoreApiDataSource.getInstance()
                .getStoreOneLottery(appToken, service, params)
                .subscribe(new ResponseObserver<BaseResponse<StoreOneLotteryBean>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<StoreOneLotteryBean> response) {
                        mView.getOneLotteryBack(true, response.data, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.getOneLotteryBack(false, null, msg);
                    }
                });
    }

    @Override
    public void checkPay(String storeId, StoreOneLotteryBean.ActivityListEntity entity,
                         String storeName, String url) {
        String cash = InfoProvider.getUserAsset(App.getInstance()).getCash_balance();
        if (!StringUtils.moneyComp(cash, "1")) {
            TipUtils.showToast(mContext, mContext.getString(R.string.home_one_lottery_pay_insufficient));
            return;
        }

        UserToken token = InfoProvider.getUserToken(App.getInstance());
        Map<String, String> map = new HashMap<>();
        map.put("activity_id", entity.getActivity_id());
        map.put("consumer_id", token.getMember_id());
        map.put("activity_type", entity.getActivity_type());
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .payOneLottery(token.getUser_token(), "goods.ldActivityPayInfoFind", params)
                .subscribe(new ResponseObserver<BaseResponse<PayInfoBean>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<PayInfoBean> response) {
                        payOrder(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.payLotteryFail(msg);
                    }
                });
    }

    private void payOrder(PayInfoBean data) {
        String userToken = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "finance.ldOrderPay";
        Map<String, String> map = new HashMap<>();
        map.put("data_source", Constants.CUSTOM_APP);
        map.put("payment_way_key", Constants.PAYMENT_WAY_05);
        Map<String, String> pay_info = data.getPay_info();
        pay_info.putAll(map);
        data.setPay_info(pay_info);
        String params = gson.toJson(pay_info);

        StoreApiDataSource.getInstance()
                .payOneLotteryOrder(userToken, service, params)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.payLotterySuccess(data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.payLotteryFail(msg);
                    }
                });
    }

}
